package com.funo.main.ctrler;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.funo.common.factory.JsonMsgFactory;
import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.pojo.TreeNode;
import com.funo.common.utils.Constant;
import com.funo.core.base.BaseCtrler;
import com.funo.main.entities.Module;
import com.funo.main.service.ModuleService;

@Controller
@RequestMapping("/module")
public class ModuleCtrler extends BaseCtrler{
	
	@Autowired
	private ModuleService moduleService; //模块service
	
	/**
	 * 获取所有菜单信息
	 * 
	 * @param id 主键ID
	 * @return
	 */
	@RequestMapping(value = "/menu")
	public @ResponseBody String menu(String id){
		List<Module> list = moduleService.findModule(id);
		return toJson(list);
	}
	
	/**
	 * 查询模块树信息
	 * 
	 * @param id 主键ID
	 * @return
	 */
	@RequestMapping(value = "/tree")
	public @ResponseBody String tree(){
		TreeNode treeNode = moduleService.moduleTree();
		return toJson(treeNode);
	}
	
	/**
	 * 查询所有下级模块信息
	 * 
	 * @param condition 查询条件集合
	 * @return
	 */
	@RequestMapping(value = "/page")
	public @ResponseBody String page(QueryCondition condition){
		PageResult<Module> page = moduleService.getModulePage(condition);
		return toJson(page);
	}
	
	/**
	 * 新增模块操作
	 * 
	 * @param session session会话
	 * @param module 模块表
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody String add(HttpSession session,Module module, String parentID) {
		
		if(sessionUser(session) == null || sessionUser(session).getId() == null){//丢失session
			return toJson(JsonMsgFactory.fail("请先登录,再进行操作!"));
		}
		
		if (module != null) {//模块不为空
			
			try {				
				
				Integer type=moduleService.addModule(module,parentID);
				
				if (type == Constant.STATUS_SUSSESS) {//成功
					return toJson(JsonMsgFactory.successMsg());
				}
				
				if (type == Constant.STATUS_REPEAT) {//重复添加
					return toJson(JsonMsgFactory.fail("请勿重复添加,添加失败"));
				}
			} catch (Exception e) {//失败
				e.printStackTrace();
				return toJson(JsonMsgFactory.failMsg());
			}
		}
		
		return null;
	}
	
	/**
	 * 修改模块操作
	 * 
	 * @param session session会话
	 * @param module 模块表
	 * @return 
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public @ResponseBody String edit(HttpSession session,Module module){
		
		if(sessionUser(session) ==null || sessionUser(session).getId() == null){//丢失session
			return toJson(JsonMsgFactory.fail("请先登录,再进行操作!"));
		}
		
		try {

			Integer type = moduleService.updateModule(module);//修改操作
			
			if (type == Constant.STATUS_SUSSESS) {
				return toJson(JsonMsgFactory.successMsg());
			}
			
			if (type == Constant.STATUS_REPEAT) {
				return toJson(JsonMsgFactory.fail("修改的信息已存在，修改失败"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
		
		return null;
	}
	
	/**
	 * 
	 * 删除模块操作
	 * 
	 * @param deleteIds ID集合
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody String delete(String deleteIds){
		
		try {

			String[]Ids=deleteIds.split(",");//传递到数据库的参数

			for (int i = 0; i < Ids.length; i++) {
				
				String id=Ids[i];
				
				List<Module> moduleList=moduleService.findParentModule(id);//通过主键ID,查找父类信息.
				
				if(moduleList.size()>0){
					return toJson(JsonMsgFactory.fail("请勿删除父类节点，删除失败"));
				}
				
				Module module=moduleService.getModule(id);//模块信息
				
				moduleService.deleteModule(module);//删除模块
			}

			return toJson(JsonMsgFactory.successMsg());
			
		} catch (Exception e) {
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	@Override
	protected String handlerException(Exception e) {
		e.printStackTrace();
		System.err.println("[handlerException:MemberController]异常类型："+e.getClass().getSimpleName());
		System.err.println(e.getMessage());
		return "系统异常";
	}
}
